//
//  YPBaseViewController.m
//  project
//
//  Created by 姚敦鹏 on 2018/10/13.
//  Copyright © 2018年 rocHome. All rights reserved.
//

#import "YPBaseViewController.h"
#import <UMAnalytics/MobClick.h>

@interface YPBaseViewController ()

@end

@implementation YPBaseViewController

-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setCurrentNavBar];
    
    if (self.isStatisticsPage) {
        
        NSString *title = strIsEmpty(self.navigationItem.title) ? (strIsEmpty(self.title) ? NSStringFromClass([self class]) : self.title) : self.navigationItem.title;
        [MobClick beginLogPageView:title];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if (self.isStatisticsPage) {
        NSString *title = strIsEmpty(self.navigationItem.title) ? (strIsEmpty(self.title) ? NSStringFromClass([self class]) : self.title) : self.navigationItem.title;
        [MobClick endLogPageView:title];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self defaultAttibuteSet];
}

- (void)setCurrentNavBar {
    
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    
    if (self.hideCurrentNavBar) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else {
        
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        
        self.navigationController.navigationBar.barTintColor = self.nowNavBarColor;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:self.nowNavBarTitleColor,NSFontAttributeName:[YPTabAndNivAttribute shareInstanll].titleFont};
        
        if (self.clearNavColor) {
            [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
        } else {
            [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:HEXColor(0xF3F3F3)]];
        }
    }
}

-(UIColor *)nowNavBarColor {
    
    if (_nowNavBarColor == nil) {
        _nowNavBarColor = [YPTabAndNivAttribute shareInstanll].navColor;
    }
    return _nowNavBarColor;
}

-(UIColor *)nowNavBarTitleColor {
    if (_nowNavBarTitleColor == nil) {
        _nowNavBarTitleColor = [YPTabAndNivAttribute shareInstanll].titleColor;
    }
    return _nowNavBarTitleColor;
}

- (void)defaultAttibuteSet {
    
    self.view.backgroundColor = self.backColor;
    
    if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }else {
        // 全屏计算 y 坐标 坐标不偏移
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

-(void)setTitle:(NSString *)title {
    self.navigationItem.title = title;
}

-(UIColor *)backColor {
    
    if (_backColor == nil) {
        _backColor = [[YPTabAndNivAttribute shareInstanll] viewBackColor];
    }
    return _backColor;
}

-(void)dealloc {
    
    // 获取当前类下 全部子类
    NSArray *arrayChildClass = [self findAll:[self class]];
    NSString *name = [NSString stringWithCString:object_getClassName([arrayChildClass lastObject]) encoding:NSUTF8StringEncoding];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    DLog(@"==== %@ 已释放 ====",name);
}

- (NSArray *)findAll:(Class)defaultClass {
    
    int count = objc_getClassList(NULL, 0);
    if (count <= 0) {
        return [NSArray arrayWithObject:defaultClass];
    }
    
    NSMutableArray *output = [NSMutableArray arrayWithObject:defaultClass];
    Class *classes = (Class *) malloc(sizeof(Class) * count);
    objc_getClassList(classes, count);
    for (NSInteger i = 0; i < count; i++) {
        if (defaultClass == class_getSuperclass(classes[i])) {
            [output addObject:classes[i]];
        }
    }
    
    return output;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
